/**
 * 
 */
package any.tag.dao.hibernate;

import java.net.URI;
import java.util.Collection;

import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import any.tag.Tag;
import any.tag.TagItem;
import any.tag.dao.TagItemDao;

/**
 * TODO: Doc me
 * 
 * @author keke <keke@codehaus.org>
 * @version
 * @revision $Revision:$
 */
public class HibernateTagItemDao extends HibernateDaoSupport implements
        TagItemDao {

    public void delete(final TagItem item) {
        getCurrentSession().delete(item);
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.tag.dao.TagItemDao#getTagItem(any.tag.Tag, java.net.URI)
     */
    public TagItem getTagItem(final Tag tag, final URI uri) {
        return (TagItem) getCurrentSession().createCriteria(TagItem.class).add(
                Restrictions.and(Restrictions.eq("tag", tag), Restrictions.eq(
                        "about", uri))).setMaxResults(1).uniqueResult();
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.tag.dao.TagItemDao#getTagItems(any.tag.Tag)
     */
    public Collection<TagItem> getTagItems(final Tag tag) {
        return getCurrentSession().createCriteria(TagItem.class).add(
                Restrictions.eq("tag", tag)).list();
    }

    public Collection<TagItem> getTagItems(final URI about) {
        return getCurrentSession().createCriteria(TagItem.class).add(
                Restrictions.eq("about", about)).list();
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.tag.dao.TagItemDao#save(any.tag.TagItem)
     */
    public void save(final TagItem item) {
        getCurrentSession().saveOrUpdate(item);

    }

    private Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

}
